package cn.itcast.c_processInstance;

import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;

public class ProcessInstanceTest {
	
	
	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
	/**
	 * 部署流程定义（从zip）
	 * 1
	 */
	@Test
	public void deploymentProcessDefinition_zip(){
		InputStream in=this.getClass().getClassLoader().getResourceAsStream("diagrams/helloworld.zip");
		ZipInputStream zipInputStream=new ZipInputStream(in);
		
		Deployment deployment = processEngine.getRepositoryService()
		.createDeployment()//创建一个部署对象
		.addZipInputStream(zipInputStream)//指定zip格式的输入流
		.deploy();
		
		System.out.println("部署ID："+deployment.getId());//
		System.out.println("部署名称："+deployment.getName());//
	}
	/**
	 * 启动流程实例
	 * 2
	 */
	@Test
	public void startProcessInstance(){
		//流程定义的key
		String processDefinitionKey = "helloworld";
		ProcessInstance pi = processEngine.getRuntimeService()//与正在执行的流程实例和执行对象相关的Service
						.startProcessInstanceByKey(processDefinitionKey);//使用流程定义的key启动流程实例，key对应helloworld.bpmn文件中id的属性值，使用key值启动，默认是按照最新版本的流程定义启动
		System.out.println("流程实例ID:"+pi.getId());//流程实例ID    101
		System.out.println("流程定义ID:"+pi.getProcessDefinitionId());//流程定义ID   helloworld:1:4
	}
	/**
	 * 查询流程状态
	 * 判断流程是正在执行还是结束
	 */
	@Test
	public void isProcessEnd(){
		
		String processid="701";
		ProcessInstance singleResult = processEngine.getRuntimeService()
						.createProcessInstanceQuery()
						.processInstanceId(processid)
						.singleResult();
		if(singleResult==null){
			System.out.println("流程已经结束");
		}else{
			System.out.println("流程没有结束");
		}
		
	}
	/**
	 * 查询历史任务
	 */
	@Test
	public void findHistoryTask(){
		String taskAssign="张三";
		List<HistoricTaskInstance> list = processEngine.getHistoryService()//与历史数据相关的service
						.createHistoricTaskInstanceQuery()//创建历史任务实例查询
						.taskAssignee(taskAssign)//指定历史任务的办理人
						.list();
		
		if(list.size()>0){
			for(HistoricTaskInstance hi:list){
				System.out.println("任务id: "+hi.getId());
				System.out.println("任务名称: "+hi.getName());
				System.out.println("流程实例id: "+hi.getProcessInstanceId());
				System.out.println("开始时间: "+hi.getStartTime());
				System.out.println("持续时间: "+hi.getDurationInMillis());
				
			}
		}
	}
	/**
	 * 查询历史流程实例
	 */
	@Test
	public void findHistoryProcessInstance(){
		
		String processid="701";
		HistoricProcessInstance singleResult = processEngine.getHistoryService()
						.createHistoricProcessInstanceQuery()
						.processInstanceId(processid)
						.singleResult();
		
		System.out.println("任务id: "+singleResult.getId());
		System.out.println("流程定义id: "+singleResult.getProcessDefinitionId());
		System.out.println("开始时间: "+singleResult.getStartTime());
		System.out.println("持续时间: "+singleResult.getDurationInMillis());
	}
}
